<?php
/**
 * 后台首页控制器
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class IndexController extends AdmincommonController {
    /**
     * 显示主页
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index () {
        isset($_SESSION['admin_name']) && $this->assign('admin_name', remove_xss($_SESSION['admin_name']));            
        $this->assign('menu',D("Menus")->getOrder());
        $this->display();
    }

    /**
     * 检测是否有权限
     * @param obj     $user      当前登录用户数据对象
     * @param string $controller 控制器
     * @param string $action     操作
     * @return bool
     * @author Antonia
     */
    public function hasPrivilege($user, $controller, $action) {
        static $auths      = null;
        static $privileges = null;

        if (is_null($privileges)) {
            if (is_null($user)) {
                return false;
            }
            $privileges = array();
            /* 群组权限合并*/
            if(!empty($user['group_id'])) {
                $groupids = explode(',', trim($user['group_id'], ','));
                $Groups = D("Admingroups");
                foreach($groupids as $gid) {
                    $rs = $Groups->where('group_id=' . $gid)->getField('group_privileges');
                    $privileges = array_merge_recursive($privileges, unserialize($rs));
                }
            }
        }
        if (!isset($privileges[$controller])) {
            return false;
        }
        if (is_null($auths)) {
            $auths = $this->parseAuthFile();
        }
        return isset($auths[$controller . '-' . $action]) &&
            in_array($auths[$controller . '-' . $action], $privileges[$controller]);
    }
}